package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.*;
import com.jdrx.sw.sewage.beans.vo.DeviceDateDisplayVO;
import com.jdrx.sw.sewage.beans.vo.GroupDeviceDisplayVO;
import com.jdrx.sw.sewage.beans.vo.ProcessunitFPVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.GatherDevPoiServiceImpl;
import com.jdrx.sw.sewage.service.ProcessunitServiceImpl;
import com.jdrx.sw.sewage.task.xxjob.SewageJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/flowchart")
@Api(value = "厂站管理-工艺流程")
public class StationFlowChartApi {

    @Autowired
    private GatherDevPoiServiceImpl gatherDevPoiServiceImpl;
    @Autowired
    private ProcessunitServiceImpl processunitServiceImpl;

    @ApiOperation(value = "根据站点id查询工艺单元")
    @RequestMapping(value = "findProcUByStationId", method = RequestMethod.POST)
    public ResponseVO<Map<String, List<ProcessunitFPVO>>> findProcUByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                               @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                               @RequestBody StationIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findProcUByStationId(dto));
    }

    @ApiOperation(value = "根据站点id查询进水/出水水质信息")
    @RequestMapping(value = "findInQualityByStationId", method = RequestMethod.POST)
    public ResponseVO<List<DeviceDateDisplayVO>> findInQualityByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                          @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                          @RequestBody @Valid JCWaterDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findInQualityByStationId(dto));
    }

    @ApiOperation(value = "根据站点id和工艺单元id查询设备展示信息")
    @RequestMapping(value = "findDisplayByProcessunitId", method = RequestMethod.POST)
    public ResponseVO<List<DeviceDateDisplayVO>> findDisplayVOByStationIdAndProcessunitId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                                          @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                                          @RequestBody StationIdAndPuIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findDisplayVOByStationIdAndProcessunitId(dto.getStationId(), null, dto.getProcessunitId()));
    }

    @ApiOperation(value = "根据设备id和工艺单元id查询点位")
    @RequestMapping(value = "findDisplayByProIdAndDevId", method = RequestMethod.POST)
    public ResponseVO<List<DeviceDateDisplayVO>> findDisplayByProIdAndDevId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                            @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                            @RequestBody DevIdAndPuIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findDisplayByProIdAndDevId(dto.getDevId(), dto.getProcessunitId()));
    }

    @ApiOperation(value = "查询站点下所有参数设置")
    @RequestMapping(value = "findParSetByStationId", method = RequestMethod.POST)
    public ResponseVO<List<GroupDeviceDisplayVO>> findParSetByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                        @RequestBody StationIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findParSetByStationId(dto.getStationId()));
    }

    @ApiOperation(value = "查询站点下所有PLC")
    @RequestMapping(value = "findPlcByStationId", method = RequestMethod.POST)
    public ResponseVO<List<GroupDeviceDisplayVO>> findPlcByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                     @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                     @RequestBody StationIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findPlcByStationId(dto.getStationId()));
    }

    @ApiOperation(value = "查询站点下所有PLC逻辑点位")
    @RequestMapping(value = "findPlcLogicByStationId", method = RequestMethod.POST)
    public ResponseVO<List<GroupDeviceDisplayVO>> findPlcLogicByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                          @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                          @RequestBody StationIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findPlcLogicByStationId(dto.getStationId()));
    }

    @ApiOperation(value = "查询站点下所有摄像头")
    @RequestMapping(value = "findCameraByStationId", method = RequestMethod.POST)
    public ResponseVO<Object> findCameraByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                    @RequestBody StationIdDTO dto) throws BizException {
        return ResponseFactory2.ok(gatherDevPoiServiceImpl.findCameraByStationId(dto.getStationId()));
    }

    @ApiOperation(value = "新增/修改工艺拖拽配置")
    @PostMapping(value = "processSet")
    public ResposeVO stationProcessSet(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                       @RequestHeader(Constant.TENANT_ID) String tenantId,
                                       @RequestBody @Validated ProcessSetTO dto) {
        processunitServiceImpl.stationProcessSet(dto);
        return ResponseFactory.ok("");
    }

    @ApiOperation(value = "获取工艺拖拽配置")
    @PostMapping(value = "findProcessSet")
    public ResposeVO findProcessSet(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                    @RequestParam("processId") Long processId) {
        return ResponseFactory.ok(processunitServiceImpl.findProcessSet(processId));
    }

    /*@ApiOperation(value = "修改设备参数值")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResponseVO updateByVO(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                 @RequestHeader(Constant.TENANT_ID) String tenantId,
                                 @RequestHeader(Constant.UID) String uid,
                                 @RequestBody GatherParVO vo) throws BizException, InterruptedException {
        vo.setLogTerminal(ELogTerminal.WEB.getKey());
        return gatherDevPoiServiceImpl.updateByVO(vo, tenantId, uid);
    }*/

    /*
    @ApiOperation(value = "下发命令测试-不要调用")
    @RequestMapping(value = "get", method = RequestMethod.POST)
    public ResponseVO command(@RequestHeader(Constant.DATA_NODE) String dataNode,
                              @RequestHeader(Constant.TENANT_ID) String tenantId,
                              @RequestParam("value") String value) throws BizException, InterruptedException {
        gatherDevPoiServiceImpl.command(value);
        return ResponseFactory2.ok();
    }*/

    @Autowired
    private SewageJob sewageJob;

    @ApiOperation(value = "下发命令测试-不要调用")
    @RequestMapping(value = "get", method = RequestMethod.POST)
    public ResponseVO command(@RequestHeader(Constant.DATA_NODE) String dataNode,
                              @RequestHeader(Constant.TENANT_ID) String tenantId,
                              @RequestParam("value") String value) throws BizException, InterruptedException {
        sewageJob.stationWaterQuality(null);
        return ResponseFactory2.ok();
    }
}
